package code.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/**
 * Created by IntelliJ IDEA.
 * User: cheny
 * Date: 12-2-29
 * Time: 下午8:25
 */
public class URLUtil {
    public static String convert(String x, String y) {
        URL url;
        URLConnection urlconn;
        try {
            url = new URL("http://api.map.baidu.com/ag/coord/convert?from=0&to=4&x=" + x + "&y=" + y);
            urlconn = url.openConnection();
            HttpURLConnection httpConnection = (HttpURLConnection) urlconn;
//            httpConnection.setConnectTimeout(1000000);
//            httpConnection.setReadTimeout(1000000);
            InputStream in = httpConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "Big5"));

            StringBuilder sb = new StringBuilder();
            String line = "";
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            in.close();

            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * BASE64解码
     */
    public static String decode(String str) {
        byte[] bt = null;
        try {
            sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
            bt = decoder.decodeBuffer(str);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return new String(bt);
    }
}
